package org.yaac.server.egql;

import java.io.Serializable;

import org.yaac.server.egql.evaluator.EvaluationResult;
import org.yaac.server.egql.evaluator.Evaluator;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
// TODO : only conditions on aggregation functions are allowed for having clause
public class HavingClause implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final Evaluator conditions;

	public HavingClause(Evaluator conditions) {
		super();
		this.conditions = conditions;
	}

	public Boolean evaluate(ProcessDataRecord record) {
		EvaluationResult result = conditions.evaluate(record);
		return (Boolean) result.getPayload();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((conditions == null) ? 0 : conditions.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		HavingClause other = (HavingClause) obj;
		if (conditions == null) {
			if (other.conditions != null)
				return false;
		} else if (!conditions.equals(other.conditions))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "HavingClause [conditions=" + conditions + "]";
	}

	public Evaluator getConditions() {
		return conditions;
	}
}
